<?php

namespace App\Services\Call;

use App\Dao\AnchorDao;
use App\Dao\CommonSettingDao;
use App\Dao\PayCallDao;
use App\Dao\UserBlacklistDao;
use App\Dao\UserDao;
use App\Model\User;
use App\Model\UserCardPack;
use App\Utils\LoggerUtil;
use App\Utils\MemcacheClient;

class CallUserService
{

    /**
     * Note - 匹配男用户
     * DateTime 2024/8/19 18:22
     * User Admin
     * @param $match_id
     * @param $uid
     * @param $call_type
     * @return array
     */
    public function matchMales($match_id, $uid, $call_type = 1)
    {
        // 获取已设置接受视频的主播，包括在线、通话中、离线三种状态的
        $refuse_list = PayCallDao::getInstance()->getRefuseByMatchId($match_id, "user_id", 41);
        $black_list = UserBlacklistDao::getInstance()->getBlackUids($uid);
        LoggerUtil::write('black_list', $black_list);
        // 获取10分钟内匹配过的用户 被拒接过的用户
        $matchUids = PayCallDao::getInstance()->getTodayMatchUid($uid, true);
        LoggerUtil::write('matchUids', $matchUids);
        $refuse_list = array_merge($refuse_list, $black_list, $matchUids);
        // $refuse_list = array_merge($refuse_list, $black_list);
        LoggerUtil::write('refuse_list', $refuse_list);
        // 匹配男用户
        $where = '';
        $push_level = User::query()->where('id', $uid)->value('push_level');
        if ($push_level <= 1) {
            $where .= "push_level in (1,2)";
            // $where['push_level']= [1,2];
        } else {
            // $where['push_level']= [1,2,3];
            $where .= "push_level in (1,2,3)";
        }
        $times = time();
        $is_over_card = 0;
        // 女用户体验卡上限
        // $anchorCardCount = $userModel->findOneField("select count(*) today_card from tb_user_card_pack where uid={$uid} and get_time>{$times}", "today_card") ?: 0;
        $anchorCardCount = UserCardPack::where('uid', $uid)->where('get_time', '>', $times)->count();
        if ($anchorCardCount >= CommonSettingDao::getValue('woman_card_count', 10)) {
            $is_over_card = 1;
        }


        [$uids, $list] = UserDao::getInstance()->getFreeMaleCallUids($refuse_list, $where, $call_type, $is_over_card);

        $total = 0;
        $data = [];
        if (!empty($uids)) {
            $uids_old = $uids;
            $get_anchor_free = MemcacheClient::getInstance()->get('im_male_free_uids'); //array，今天已经有接过免费视频的主播
            $get_anchor_free_date = MemcacheClient::getInstance()->get('im_male_free_uids_date'); //日期
            !is_array($get_anchor_free) && $get_anchor_free = array();
            $date = date("Y-M-d");
            if ($get_anchor_free_date != $date) {
                MemcacheClient::getInstance()->set('im_male_free_uids_date', $date, 24 * 3600);
                $get_anchor_free = array();
            }
            if (!empty($get_anchor_free)) {
                $uids = array_diff($uids, $get_anchor_free);
                if (empty($uids)) { //重置
                    $uids = $uids_old;
                    $get_anchor_free = array();
                }
            }

            // $payCallModel = new \PayCallModel();
            foreach ($uids as $k => $v) {
                //看是否在通话中
                if (!PayCallDao::getInstance()->checkIsCalling($v, false)) { //有缓存
                    $total = 1;
                    $data = array(
                        'uid' => $list[$v]['uid'],
                        'head_image' => $list[$v]['head_image'],
                        'nickname' => $list[$v]['nickname'],
                        'sex' => $list[$v]['sex'],
                        'video_price' => 0,
                        'audio_price' => 0,
                        'accept_video' => 1,
                        'accept_audio' => 1,
                        'level' => $list[$v]['level'],
                        'province' => $list[$v]['province'],
                        'city' => $list[$v]['city'],
                        'status' => 0,
                        'gift_count' => $list[$v]['gift_count'],
                    );
                    $get_anchor_free[] = $v;
                    MemcacheClient::getInstance()->set('im_male_free_uids', $get_anchor_free, 24 * 3600);
                    break;
                }
            }
        }

        return [$total, $data];
    }


    /**
     * Note - 匹配女用户
     * DateTime 2024/8/19 18:22
     * User Admin
     * @param $match_id
     * @param $uid
     * @param $call_type
     * @return array
     */
    public function matchFemales($match_id, $uid, $call_type = 1)
    {
        //获取已设置接受视频的主播，包括在线、通话中、离线三种状态的
        $refuse_list = PayCallDao::getInstance()->getRefuseByMatchId($match_id, "anchor_id", 31);
        $black_list = UserBlacklistDao::getInstance()->getBlackUids($uid);

        // $userModel = new \UserModel();
        // vip 可重复匹配同
        // $userInfo = $userModel->find("id,super_noble,noble", 'id=?', [$uid]);
        // $userInfo = User::query()->select(['id','super_noble','noble'])->where('id', $uid)->first();
        $refuse_list = array_merge($refuse_list, $black_list);


        // 判断当前男用户是否可以使用体验卡
        $can_use_card = 0;
        // $cardData = $userModel->find('*', "uid=? and card_type=? and status=?", [$uid, 1, 0], "", "tb_user_card_pack");
        $cardData = UserCardPack::query()->where('uid', $uid)->where('card_type', 1)->where('status', 0)->first();
        if (!empty($cardData)) {
            // $useCard = $userModel->find('*', "uid=? and card_type=? and status=1 and use_time>?", [$uid, 1, strtotime(date("Ymd"))], "", "tb_user_card_pack");
            $useCard = UserCardPack::query()->where('uid', $uid)
                ->where('card_type', 1)
                ->where('status', 1)->where('use_time', '>', strtotime(date("Ymd")))
                ->first();
            if (empty($useCard)) {
                $can_use_card = 1;
            }
        }

        logInfo("refuse", [$uid, $match_id, $refuse_list], false);
        [$uids, $list] = AnchorDao::getInstance()->getFreeFemaleCallUids($refuse_list, 0, $call_type, $can_use_card);

        $total = 0;
        $data = [];

        if (!empty($uids)) {
            $uids_old = $uids;

            $get_anchor_free = MemcacheClient::getInstance()->get('im_anchor_free_uids'); //array，今天已经有接过免费视频的主播
            $get_anchor_free_date = MemcacheClient::getInstance()->get('im_anchor_free_uids_date'); //日期
            !is_array($get_anchor_free) && $get_anchor_free = array();
            $date = date("Y-M-d");
            if ($get_anchor_free_date != $date) {
                MemcacheClient::getInstance()->set('im_anchor_free_uids_date', $date, 24 * 3600);
                $get_anchor_free = array();
            }
            if (!empty($get_anchor_free)) {
                $uids = array_diff($uids, $get_anchor_free);
                if (empty($uids)) { //重置
                    $uids = $uids_old;
                    $get_anchor_free = array();
                }
            }

            foreach ($uids as $k => $v) {
                //看是否在通话中
                if (!PayCallDao::getInstance()->checkIsCalling($v)) { //有缓存
                    $total = 1;
                    $data = array(
                        'uid' => $list[$v]['uid'],
                        'head_image' => $list[$v]['head_image'],
                        'nickname' => $list[$v]['nickname'],
                        'sex' => $list[$v]['sex'],
                        'video_price' => $list[$v]['video_price'],
                        'call_video' => $list[$v]['call_video'],
                        'audio_price' => $list[$v]['audio_price'],
                        'accept_video' => $list[$v]['accept_video'],
                        'accept_audio' => $list[$v]['accept_audio'],
                        'level' => $list[$v]['level'],
                        'province' => $list[$v]['province'],
                        'city' => $list[$v]['city'],
                        'status' => 0,
                        'gift_count' => 0,
                    );
                    $get_anchor_free[] = $v;
                    MemcacheClient::getInstance()->set('im_anchor_free_uids', $get_anchor_free, 24 * 3600);
                    break;
                }
            }
        }

        return [$total, $data];
    }


}